package com.lancoo.answer.widget.combinationView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.entity.TaskControlBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2021/12/14 10:58
 * Description:参考答案查看ViewPager组合View
 */
public class AnswerAnalysisVPView extends RelativeLayout {

    List<Item> itemList;
    private AnalysisAdapter mAdapter;
    private ViewPager2 viewPager2;
    private OnAnswerAnalysisPageChangeCallback onAnswerAnalysisPageChangeCallback;

    public AnswerAnalysisVPView(Context context) {
        super(context);
        init(context);
    }

    public AnswerAnalysisVPView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AnswerAnalysisVPView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        itemList=new ArrayList<>();
        LayoutInflater.from(context).inflate(R.layout.ev_layout_answer_vp_analysis,this);
        viewPager2 = findViewById(R.id.viewpager2);
        mAdapter=new AnalysisAdapter(itemList);
        viewPager2.setAdapter(mAdapter);
        viewPager2.setUserInputEnabled(false);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(getContext()==null){
                    return;
                }
                if(onAnswerAnalysisPageChangeCallback!=null){
                    onAnswerAnalysisPageChangeCallback.onPageSelect(position);
                }
            }
        });
    }


    /**
     *
     * @param list list
     * @param loadItemIndex index
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setItemList(List<Item> list, int loadItemIndex){
        itemList.clear();
        itemList.addAll(list);
        if(mAdapter!=null){
            mAdapter.notifyDataSetChanged();
        }

        if(loadItemIndex>=0){
            viewPager2.setCurrentItem(loadItemIndex,false);
        }
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean!=null&&taskControlBean.getTrainSence()!=0){
            viewPager2.setVisibility(GONE);
        }
    }

    public void setViewPager2CurrentIndex(int position){
        viewPager2.setCurrentItem(position,true);
    }

    public void setOnAnswerAnalysisPageChangeCallback(OnAnswerAnalysisPageChangeCallback onAnswerAnalysisPageChangeCallback) {
        this.onAnswerAnalysisPageChangeCallback = onAnswerAnalysisPageChangeCallback;
    }

    private static class AnalysisAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

        private final List<Item> itemList;

        public AnalysisAdapter(List<Item> itemList) {
            this.itemList = itemList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate=LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.ev_item_layout_answer_analysis,parent,false);
            return new Vh(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            Vh vh= (Vh) holder;
            vh.initData(itemList.get(position));
        }

        @Override
        public int getItemCount() {
            return itemList.size();
        }

        public static class Vh extends RecyclerView.ViewHolder{

            public Vh(@NonNull View itemView) {
                super(itemView);
            }

            private void initData(Item item){
                AnswerAnalysisView answerAnalysisView=itemView.findViewById(R.id.answer_analysis_view);
                String itemAnalysis = item.getItemAnalysis();
                answerAnalysisView.adjustFontSize();
                answerAnalysisView.setAnalysis(itemAnalysis);

            }
        }
    }

    public abstract static class OnAnswerAnalysisPageChangeCallback{

        public void onPageSelect(int position){}
    }

    public void adjustFontSize(){
        if(mAdapter!=null){
            mAdapter.notifyDataSetChanged();
        }
    }
}
